
# Create separate maps for each year in all boroughs
maps_all_boroughs <- list()
for (year in 2015:2019) {
data_year <- rat_sightings_data %>%
filter(year_created == year, !is.na(latitude), !is.na(longitude))
maps_all_boroughs[[as.character(year)]] <- leaflet(data_year) %>%
addTiles() %>%
addCircleMarkers(
lng = ~longitude,
lat = ~latitude,
radius = 5,
popup = ~paste("Location Type", location_type, "<br>Date: ", paste(month_created, year_created, sep = "-"))
) %>%
addLegend("bottomright", colors = "red", labels = paste("Rat Sightings - ", year)) %>%
addMarkers(
clusterOptions = markerClusterOptions()
)
}
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
# Display each map separately for all boroughs and all years
names(maps_all_boroughs) <- paste("Rat Sightings -", 2015:2019)
maps_all_boroughs
## $`Rat Sightings - 2015`
##
## $`Rat Sightings - 2016`
##
## $`Rat Sightings - 2017`
##
## $`Rat Sightings - 2018`
##
## $`Rat Sightings - 2019`
lapply(names(maps_all_boroughs), function(year) {
maps_all_boroughs[[year]] %>%
leaflet(width = "100%", height = "500px") %>%
setView(lng = -73.97, lat = 40.78, zoom = 10) %>%
addTiles()
})
## [[1]]
##
## [[2]]
##
## [[3]]
##
## [[4]]
##
## [[5]]
maps_all_boroughs[["Rat Sightings - 2015"]]
maps_all_boroughs[["Rat Sightings - 2016"]]
maps_all_boroughs[["Rat Sightings - 2017"]]
maps_all_boroughs[["Rat Sightings - 2018"]]
maps_all_boroughs[["Rat Sightings - 2019"]]
# Filter data for the years 2015-2019
barplot_rat_data <- rat_sightings_data %>%
filter(year_created >= 2015 & year_created <= 2019)
# Calculate the average annual rat sightings by borough
average_sightings_by_borough <- barplot_rat_data %>%
group_by(year_created, borough) %>%
summarise(average_sightings = n() / length(unique(month_created)))
## `summarise()` has grouped output by 'year_created'. You
## can override using the `.groups` argument.
# Create a bar plot
ggplot(average_sightings_by_borough, aes(x = borough, y = average_sightings, fill = factor(year_created))) +
geom_bar(stat = "identity", position = "dodge", color = "black") +
labs(title = "Average Annual Rat Sightings by Borough (2015-2019)",
x = "Borough",
y = "Average Annual Rat Sightings") +
scale_fill_manual(name = "Year", values = c("2015" = "skyblue", "2016" = "orange", "2017" = "pink", "2018" = "purple", "2019" = "red")) +
theme_minimal()

# Filter data for the years 2015-2019
filtered_rat_data <- rat_sightings_data %>%
filter(year_created >= 2015 & year_created <= 2019)
# Line plot for the number of rat sightings by month with points
ggplot(filtered_rat_data, aes(x = month_created, group = year_created)) +
geom_line(aes(y = after_stat(count), color = factor(year_created)), stat = "count") +
geom_point(aes(y = after_stat(count), color = factor(year_created)), stat = "count", size = 2) +
labs(title = "Number of Rat Sightings by Month (2015-2020)",
x = "Month",
y = "Number of Rat Sightings") +
scale_x_discrete(labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) +
scale_color_discrete(name = "Year") +
theme_minimal()
